package com.school.lab.web.action.admin;

import java.util.List;


import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.school.lab.common.utils.Pager;
import com.school.lab.domain.Acl;
import com.school.lab.domain.Moudel;
import com.school.lab.domain.Role;
import com.school.lab.domain.User;
import com.school.lab.service.AclService;
import com.school.lab.service.MoudelService;
import com.school.lab.service.RoleService;
import com.school.lab.service.UserService;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String doType;

	private Pager pager;

	private int pageSize = 20;

	private User user;

	private UserService userService;

	private RoleService roleService;

	private AclService aclService;

	private MoudelService moudelService;

	private Long id;

	private String[] gradeList;

	private String[] subjectList;

	private String userType;


	private Role roleTemp;


	private Long schoolId;


	private String username;

	private String operaterTime;



	public String toCreateUser() throws Exception {
			return "frontStudent";

	}

	public String toEditUser() throws Exception {
		this.setDoType("update");
			return "frontStudent";
	}

	public String findRole() throws Exception {
		return "roleList";
	}

	public String authRole() throws Exception {
		Role role = roleService.getRoleById(id);
		List<Moudel> moudels = moudelService.findMoudels();
		List<Acl> aclList = aclService.findAclByRole(id);
		if (aclList.size() != 0) {
			for (Acl acl : aclList) {
				Moudel moudel = acl.getMoudel();
				for (Moudel m : moudels) {
					if (m.getAclVo() == null) {
						if (m.getId().equals(moudel.getId())) {
							m.setAclVo(acl);
						}
					}
				}
			}
		}
		ActionContext.getContext().getSession().put("role", role);
		ActionContext.getContext().getSession().put("moudels", moudels);
		return SUCCESS;
	}

	public String listUser() throws Exception {
		return SUCCESS;
	}

	public String findUser() throws Exception {
		return SUCCESS;
	}

	public String editRole() throws Exception {
//		if (roleTemp.getAdmin().equals("0")) {
//			roleTemp.setAdmin("admin");
//		}
		roleService.saveRole(roleTemp);
		addActionMessage("添加成功");
		return "role";
	}

	public String editUser() throws Exception {
		return SUCCESS;
	}

	public String delUser() throws Exception {
		try {
			 int start =(Integer)ActionContext.getContext().getSession().get("start");
 			ActionContext.getContext().put("start", start);
			String userType = ServletActionContext.getRequest().getParameter(
					"userType");
			ActionContext.getContext().getSession().put("userType", userType);
			this.user=this.userService.getUserById(id);
			this.userService.updateUser(user);
			ActionContext.getContext().getSession().put("message", "删除成功");
			return SUCCESS;
		} catch (RuntimeException e) {
			ActionContext.getContext().getSession().put("message",
					"该用户正在其他模块被使用无法删除");
			return SUCCESS;
		}
	}

	public String login() throws Exception {
		
		return SUCCESS;
	}

	public String index() throws Exception {
		return SUCCESS;
	}

	
	public String toUpdateIntegral() throws Exception {
		this.user = userService.getUserById(id);
		return SUCCESS;
	}

	public String toHome() throws Exception {
		String userType = ServletActionContext.getRequest().getParameter(
				"userType");
		if ("admin".equals(userType)) {
			return "adminBack";
		} else {
			return "teacherBack";
		}
	}

	public String exit() throws Exception {
		ActionContext.getContext().getSession().clear();
		return SUCCESS;
	}

	
	public String getDoType() {
		return doType;
	}

	public void setDoType(String doType) {
		this.doType = doType;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}


	public String[] getGradeList() {
		return gradeList;
	}

	public void setGradeList(String[] gradeList) {
		this.gradeList = gradeList;
	}

	public String[] getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(String[] subjectList) {
		this.subjectList = subjectList;
	}

	public void setAclService(AclService aclService) {
		this.aclService = aclService;
	}

	public void setMoudelService(MoudelService moudelService) {
		this.moudelService = moudelService;
	}

	public Role getRoleTemp() {
		return roleTemp;
	}

	public void setRoleTemp(Role roleTemp) {
		this.roleTemp = roleTemp;
	}


	public Long getSchoolId() {
		return schoolId;
	}

	public void setSchoolId(Long schoolId) {
		this.schoolId = schoolId;
	}


	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getOperaterTime() {
		return operaterTime;
	}

	public void setOperaterTime(String operaterTime) {
		this.operaterTime = operaterTime;
	}
}
